import java.io.*;
import java.net.*;

import javax.swing.SwingUtilities;

public class ConnectionThread extends Thread {

	private Socket m_ClientSocket;
	private MainWindow m_MainWidow;
	private Runnable m_Runx;
	private String clientId;

	public ConnectionThread(Socket clientSocket, MainWindow mainwindow) {
		m_ClientSocket = clientSocket;
		m_MainWidow = mainwindow;
		m_Runx = new Runnable() {
			public void run() {
				m_MainWidow.addClientTreeNode(clientId);
			}
		};
	}

	public void run() {
		String m_SrcIP = m_ClientSocket.getInetAddress().toString();
		int m_DestPort = m_ClientSocket.getPort();
		System.out.println("From:" + m_SrcIP + " port:" + m_DestPort);

		while (true) {
			try {
				BufferedReader bufferedReader = new BufferedReader(
						new InputStreamReader(m_ClientSocket.getInputStream()));
 				clientId = bufferedReader.readLine();
				
				ClientCenter clientCenter = ClientCenter.createClientCenter();
				Client client = clientCenter.createClient(clientId,
						m_ClientSocket);
				
				System.out.println("client id:" + client.getId());
				
				SwingUtilities.invokeLater(m_Runx);
				return;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
